package com.ponhu.ea2.stockcenter.config;

import com.ponhu.ea2.common.basic.model.ResponseBean;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.support.DefaultMessageSourceResolvable;
import org.springframework.stereotype.Component;
import org.springframework.validation.BindException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

import java.util.stream.Collectors;

@Slf4j
@RestControllerAdvice
@Component
public class GlobalExceptionHandler {

    //校验框架异常处理
    @ExceptionHandler(BindException.class)
    public ResponseBean validatedBindException(BindException e) {
        String msg = e.getAllErrors().stream()
                .map(DefaultMessageSourceResolvable::getDefaultMessage)
                .collect(Collectors.joining("；"));
        return ResponseBean.error(msg);
    }

}
